<?php
/**
 * 工厂类
 * @author Tongle Xu <xutongle@gmail.com> 2013-9-14
 * @copyright Copyright (c) 2003-2103 tintsoft.com
 * @license http://www.tintsoft.com
 * @version $Id$
 */
class Factory {

	/**
	 *
	 * @param unknown $className
	 * @param unknown $args
	 */
	public static function createInstance($className, $args = array()) {

	}

	/**
	 * (non-PHPdoc)
	 * @see IFactory::getInstance()
	 */
	public function getInstance($alias, $args = array()) {

	}

	/**
	 * 解析组件对象构造方法参数信息,并返回参数列表
	 *
	 * 该方法解析组件对象构造方法的参数信息,相关的组件配置<code><constructor-args>
	 * <constructor-arg name='0' value='DATA:log' />
	 * <constructor-arg name='1' value='2' />
	 * </constructor-args></code>相关定义同properties相同.'name'为参数位置,生成的参数列表按照'name'一次排序.
	 * @param array $constructors
	 * @param array $args
	 * @return void
	 */
	protected function buildArgs($constructors, &$args) {
		foreach ((array) $constructors as $key => $_var) {
			$key = intval($key);
			if (isset($_var['value'])) {
				$args[$key] = $_var['value'];
			} elseif (isset($_var['ref']))
			$args[$key] = $this->getInstance($_var['ref']);
			elseif (isset($_var['path'])) {
				$_className = Kernel::import($_var['path']);
				$args[$key] = $this->createInstance($_className);
			}
		}
		ksort($args);
	}

	/**
	 * 执行类的初始化方法
	 *
	 * 类的初始化方法的组件定义<code><component init-method=''>...</component></code>
	 * @param string $initMethod
	 * @param object $instance
	 * @return mixed
	 * @throws Base_Exception
	 */
	protected function executeInitMethod($initMethod, $instance) {
		try {
			return call_user_func_array(array($instance, $initMethod), array());
		} catch (Exception $e) {
			throw new Base_Exception(
					'[Factory.executeInitMethod] (' . $initMethod . ', ' . $e->getMessage() . ')',
					WindException::ERROR_CLASS_METHOD_NOT_EXIST);
		}
	}
}